<?php declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020/5/9
 * Time: 16:05
 */

namespace App\Rpc\Validator;


use Swoft\Validator\Annotation\Mapping\IsInt;
use Swoft\Validator\Annotation\Mapping\IsString;
use Swoft\Validator\Annotation\Mapping\Validator;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\Contract\ValidatorInterface;
use Swoft\Bean\BeanFactory;
use App\Rpc\Model\Dao\OrganizationDao;
use App\Lib\Error\ErrorConstant;

/**
 * Class OrganizationValidator
 *
 * @since 2.0
 *
 * @Validator(name="OrganizationValidator")
 *
 * @package App\Validator
 */
class OrganizationValidator implements ValidatorInterface
{
    /**
     * @IsString(message="item_id类型不正确")
     * @var string
     */
    protected $item_id;

    /**
     * @IsString(message="角色名称类型不正确")
     *
     * @var string
     */
    protected $name;

    /**
     * @IsString(message="角色权限值不正确")
     *
     * @var string
     */
    protected $permission_value;

    /**
     * @IsString(message="请输入正确的角色id")
     *
     * @var string
     */
    protected $role_id;

    /**
     * @IsInt(message="冻结状态类型不正确")
     *
     * @var int
     */
    protected $frozen_state;

    /**
     * @IsInt(message="创建时间不合法")
     *
     * @var int
     */
    protected $created_at;

    /**
     * @IsInt(message="更新时间不合法")
     *
     * @var int
     */
    protected $updated_at;

    /**
     * @param array $data
     * @param array $params
     * @return array
     * @throws ValidatorException
     */
    public function validate(array $data, array $params): array
    {
        $op      = $params['op']??null;
        $valid_item_id = $data['item_id']??null;
        $valid_permission_value = $data['permission_value']??null;
        $valid_name = $data['name']??null;
        $role_id   = $data['role_id']??null;

        if(empty($valid_item_id) && $op == 'add'){
            throw new ValidatorException("唯一item_id必须填写", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        if($op == 'add' && strlen($valid_item_id) != 32 ){
            throw new ValidatorException("唯一item_id格式不正确", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        //验证是否存在
        if($op == 'add' && !$valid_name){
            throw new ValidatorException("请输入组织名称", ErrorConstant::VALIDATE_ERROR_CODE);
        }
        if($valid_name){
            //验证permission_value是否唯一
            $Dao = BeanFactory::getBean(OrganizationDao::class);
            $first = $Dao->getByWhere(['name' => $valid_name]);
            if($first){
                throw new ValidatorException("组织名称重复请重新输入", ErrorConstant::VALIDATE_ERROR_CODE);
            }
        }

        //验证是否存在
        if($op == 'add' && !$valid_permission_value){
            throw new ValidatorException("请输入组织权限唯一值", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        if($valid_permission_value){
            //验证permission_value是否唯一
            $Dao = BeanFactory::getBean(OrganizationDao::class);
            $first = $Dao->getByWhere(['permission_value' => $valid_permission_value]);
            if($first){
                throw new ValidatorException("组织权限重复请重新输入", ErrorConstant::VALIDATE_ERROR_CODE);
            }
        }

        //验证角色
        if($role_id ){
            if(strlen($role_id) !=  32){
                throw new ValidatorException("角色结构错误请重新输入", ErrorConstant::VALIDATE_ERROR_CODE);
            }
            //继续验证是否重复
            $Dao = BeanFactory::getBean(OrganizationDao::class);
            $first = $Dao->getByWhere(['item_id' => $valid_item_id]);
            if($first){
                throw new ValidatorException("角色已经绑定请勿重复绑定", ErrorConstant::VALIDATE_ERROR_CODE);
            }
        }

        return $data;
    }
}